package cn.tedu.boot35.controller;

import cn.tedu.boot35.entity.User;
import cn.tedu.boot35.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class UserController {

    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        System.out.println("user = " + user);
        User u = mapper.select(user.getUsername());
        System.out.println("u = " + u);
        if (u!=null){
            return 1;
        }
        mapper.insert(user);
        return 2;
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        System.out.println("user = " + user);
        User u = mapper.select(user.getUsername());
        System.out.println("u = " + u);
        if (u!=null){
            if (user.getPassword().equals(u.getPassword())){
                return 1;
            }
            return 2;
        }
        return 3;
    }

    String name;
    @RequestMapping("/update")
    public int update(@RequestBody User user){
        User u = mapper.select(user.getUsername());
        if (u!=null){
            name = u.getUsername();
            return 1;
        }
        return 2;
    }

    @RequestMapping("/pswd")
    public int pswd(@RequestBody User user1){
        System.out.println("user1 = " + user1);
        User u = mapper.select(name);
        System.out.println("u = " + u);
        if (u.getPassword().equals(user1.getPassword())){
            mapper.updatePassword(name,user1.getPassword1());
            return 1;
        }
        return 2;
    }
}
